/********************************************************************
FileName    :   Dal_web_cart.cs
CreateTime  :   2015-04-30 03:04
BaseClass   :   Dal_web_cart
FileType    :   .cs
Author      :  minjie
Description :   Dal_web_cartDAL层（简单三层）
*********************************************************************/

using System;
using System.Text;
using System.Data.SqlClient;
using System.Collections.Generic;

using System.Data;
namespace OS.DAL
{
	/// <summary>
	/// Dal_web_cart的摘要说明。
	/// </summary>
	public partial class Dal_web_cart 
  	{ 
     		      
  		public bool Exists(int  cart_id) 
  		{ 
  			StringBuilder strSql=new StringBuilder(); 
  			strSql.Append("select count(1) from web_cart"); 
  			strSql.Append(" where "); 
           SqlParameter[] parameters = { new SqlParameter("@cart_id", SqlDbType.Int,4)};  
  			return DbHelperSQL.Exists(strSql.ToString(),parameters); 
  		}

        /// <summary>
        /// 是否存在
        /// </summary>
        /// <param name="tmh"></param>
        /// <param name="userSn"></param>
        /// <returns></returns>
        public bool Exists(string tmh, string userSn)
        {
            string sql = "select product_quantity from web_cart where product_sn='" + tmh + "'and user_sn='" + userSn + "'";
            object obj = DbHelperSQL.GetSingle(sql);

            bool isExist = false;

            if (obj != null && obj.ToString() != "")
            {
                if (int.Parse(obj.ToString()) > 0)
                    isExist = true;
            }
            return isExist;
        }

        /// <summary>
        /// 获取行号
        /// </summary>
        /// <param name="userSn"></param>
        /// <returns></returns>
        public int GetNewCartHH(string userSn)
        {
            string sql = "select top 1 hh  from web_cart where user_sn='" + userSn + "' order by hh desc";

            int rowIndex = 0;

            object obj = DbHelperSQL.GetSingle(sql);

            if (obj != null && obj.ToString() != "")
            {
                rowIndex = int.Parse(obj.ToString());
            }

            rowIndex++;
            return rowIndex;
        }



  				 
  		 
  		/// <summary> 
  		/// 增加一条数据 
  		/// </summary> 
  		public  bool  Add(web_cart_Entity model) 
  		{ 
  			StringBuilder strSql=new StringBuilder(); 
  			strSql.Append("insert into web_cart(");			 
  	strSql.Append("product_sn,product_quantity,user_sn,cart_add_time,hh,status,sm) values ("); 
  	strSql.Append("	@product_sn,@product_quantity,@user_sn,@cart_add_time,@hh,@status,@sm	  "); 
  			SqlParameter[] parameters = { 
              new SqlParameter("@product_sn", SqlDbType.VarChar,50) ,              
              new SqlParameter("@product_quantity", SqlDbType.Int,4) ,              
              new SqlParameter("@user_sn", SqlDbType.VarChar,50) ,              
              new SqlParameter("@cart_add_time", SqlDbType.DateTime) ,              
              new SqlParameter("@hh", SqlDbType.Int,4) ,              
              new SqlParameter("@status", SqlDbType.Int,4) ,              
              new SqlParameter("@sm", SqlDbType.VarChar,50)};             
              parameters[1].Value = model.product_sn;  
              parameters[2].Value = model.product_quantity;  
              parameters[3].Value = model.user_sn;  
              parameters[4].Value = model.cart_add_time;  
              parameters[5].Value = model.hh;  
              parameters[6].Value = model.status;  
              parameters[7].Value = model.sm;  
  			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);			 
  			if (rows > 0) 
  			{ 
  				return true; 
  			} 
  			else 
  			{ 
              return false;; 
           }  
  		} 
  		 
  		 
  		/// <summary> 
  		/// 更新一条数据 
  		/// </summary> 
  		public bool Update(web_cart_Entity model) 
  		{ 
  			StringBuilder strSql=new StringBuilder(); 
  			strSql.Append("update web_cart set "); 
              strSql.Append(" product_sn = @product_sn,"); 
              strSql.Append(" product_quantity = @product_quantity,"); 
              strSql.Append(" user_sn = @user_sn,"); 
              strSql.Append(" cart_add_time = @cart_add_time,"); 
              strSql.Append(" hh = @hh,"); 
              strSql.Append(" status = @status,"); 
              strSql.Append(" sm = @sm"); 
  			strSql.Append(" where  cart_id=@cart_id "); 
  						 
  SqlParameter[] parameters = { 
              new SqlParameter("@product_sn", SqlDbType.VarChar,50) ,              
              new SqlParameter("@product_quantity", SqlDbType.Int,4) ,              
              new SqlParameter("@user_sn", SqlDbType.VarChar,50) ,              
              new SqlParameter("@cart_add_time", SqlDbType.DateTime) ,              
              new SqlParameter("@hh", SqlDbType.Int,4) ,              
              new SqlParameter("@status", SqlDbType.Int,4) ,              
              new SqlParameter("@sm", SqlDbType.VarChar,50)};             
              parameters[1].Value = model.product_sn;  
              parameters[2].Value = model.product_quantity;  
              parameters[3].Value = model.user_sn;  
              parameters[4].Value = model.cart_add_time;  
              parameters[5].Value = model.hh;  
              parameters[6].Value = model.status;  
              parameters[7].Value = model.sm;  
              int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters); 
  			if (rows > 0) 
  			{ 
  				return true; 
  			} 
  			else 
  			{ 
  				return false; 
  			} 
  		} 
  		 
  		 
  		/// <summary> 
  		/// 删除一条数据 
  		/// </summary> 
  		public bool Delete(int  cart_id) 
  		{ 
  			 
  			StringBuilder strSql=new StringBuilder(); 
  			strSql.Append("delete from strTableName "); 
  			strSql.Append(" where  cart_id=@cart_id"); 
  			SqlParameter[] parameters = {  new SqlParameter("@cart_id", SqlDbType.BigInt,8) };  
    parameters[0].Value = cart_id;  
   
  			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters); 
  			if (rows > 0) 
  			{ 
  				return true; 
  			} 
  			else 
  			{ 
  				return false; 
  			} 
  		} 
  		 
  		/// <summary> 
  		/// 批量删除一批数据 
  		/// </summary> 
  		public bool DeleteList(string cart_idlist ) 
  		{ 
  			StringBuilder strSql=new StringBuilder(); 
  			strSql.Append("delete from web_cart "); 
  			strSql.Append(" where ID in ("+cart_idlist + ")  "); 
  			int rows=DbHelperSQL.ExecuteSql(strSql.ToString()); 
  			if (rows > 0) 
  			{ 
  				return true; 
  			} 
  			else 
  			{ 
  				return false; 
  			} 
  		} 
  		 
  		 
  		/// <summary> 
  		/// 得到一个对象实体 
  		/// </summary> 
  		public web_cart_Entity GetModel(int   cart_id) 
  		{ 
  			 
  			StringBuilder strSql=new StringBuilder(); 
  			strSql.Append("select  product_sn,product_quantity,user_sn,cart_add_time,hh,status,sm");	
		strSql.Append("  from web_cart"); 
  			strSql.Append(" where  cart_id=@cart_id "); 
  			SqlParameter[] parameters = {  new SqlParameter("@cart_id", SqlDbType.BigInt,8) };  
    parameters[0].Value = cart_id;  
  			 
  			web_cart_Entity model=new web_cart_Entity(); 
  			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters); 
  			 
  			if(ds!=null&&ds.Tables[0].Rows.Count>0) 
  			{ 
  		DataTable dt=ds.Tables[0];	  
  				model.product_sn= dt.Rows[0]["product_sn"].ToString(); 
  				if(dt.Rows[0]["product_quantity"]!=null&&dt.Rows[0]["product_quantity"].ToString()!="") 
  				{ 
  					model.product_quantity=int.Parse(dt.Rows[0]["product_quantity"].ToString()); 
  				} 
  				model.user_sn= dt.Rows[0]["user_sn"].ToString(); 
  				if(dt.Rows[0]["cart_add_time"]!=null&&dt.Rows[0]["cart_add_time"].ToString()!="") 
  				{ 
  					model.cart_add_time=DateTime.Parse(dt.Rows[0]["cart_add_time"].ToString()); 
  				} 
  				if(dt.Rows[0]["hh"]!=null&&dt.Rows[0]["hh"].ToString()!="") 
  				{ 
  					model.hh=int.Parse(dt.Rows[0]["hh"].ToString()); 
  				} 
  				if(dt.Rows[0]["status"]!=null&&dt.Rows[0]["status"].ToString()!="") 
  				{ 
  					model.status=int.Parse(dt.Rows[0]["status"].ToString()); 
  				} 
  				model.sm= dt.Rows[0]["sm"].ToString(); 
  				return model; 
  			} 
  			else 
  			{ 
  				return null; 
  			} 
  		} 
  		 
  		/// <summary> 
  		/// 分页获取数据列表 
  		/// </summary> 
  		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex) 
  		{ 
  			StringBuilder strSql=new StringBuilder(); 
  		 strSql.Append("SELECT * FROM ( "); 
  		 strSql.Append(" SELECT ROW_NUMBER() OVER ("); 
  if (!string.IsNullOrEmpty(orderby.Trim())) 
 { 
    strSql.Append("order by T." + orderby); 
 } 
else  
 { 
     strSql.Append("order by T.cart_id desc"); 
 } 
		 strSql.Append(")AS Row, T.*  from web_cart T "); 
  		 if (!string.IsNullOrEmpty(strWhere.Trim()))  
  			{ 
  			 strSql.Append(" WHERE " + strWhere); 
  			} 
  		    strSql.Append(" ) TT"); 
  	        strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);			return DbHelperSQL.Query(strSql.ToString()); 
  		} 
  		 
  		 
  		/// <summary> 
  		/// 获得数据列表 
  		/// </summary> 
  		public DataSet GetList(string strWhere) 
  		{ 
  			StringBuilder strSql=new StringBuilder(); 
  			strSql.Append("select * "); 
  			strSql.Append(" FROM web_cart "); 
  			if(strWhere.Trim()!="") 
  			{ 
  				strSql.Append(" where "+strWhere); 
  			} 
  			return DbHelperSQL.Query(strSql.ToString()); 
  		} 
  		 
  		/// <summary> 
  		/// 获得前几行数据 
  		/// </summary> 
  		public DataSet GetList(int Top,string strWhere,string filedOrder) 
  		{ 
  			StringBuilder strSql=new StringBuilder(); 
  			strSql.Append("select "); 
  			if(Top>0) 
  			{ 
  				strSql.Append(" top "+Top.ToString()); 
  			} 
  			strSql.Append(" * "); 
  			strSql.Append(" FROM web_cart "); 
  			if(strWhere.Trim()!="") 
  			{ 
  				strSql.Append(" where "+strWhere); 
  			} 
  			strSql.Append(" order by " + filedOrder); 
  			return DbHelperSQL.Query(strSql.ToString()); 
  		} 
  		 
  		/// <summary> 
  		/// 获得记录条数 
  		/// </summary> 
  		public int GetRecordCount(string strWhere) 
  		{ 
  			StringBuilder strSql=new StringBuilder(); 
  			strSql.Append("select count(1) "); 
  			strSql.Append(" FROM web_cart "); 
  			if(strWhere.Trim()!="") 
  			{ 
  				strSql.Append(" where "+strWhere); 
  			} 
  		   object obj=	DbHelperSQL.GetSingle(strSql.ToString()); 
  			if(obj!=null && obj.ToString()!="") 
  			{ 
              	return Convert.ToInt32(obj); 
  			} 
  			else 
  			return 0; 
  		} 
   
      
  	} 
  
}
